package com.example.mybluetooth;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.mybluetooth.adapter.BleConnectionAdapter;
import com.example.mybluetooth.adapter.BleConnectionHelper;

public class BleClientPageActivity extends FragmentActivity {

    static final String Extra_MacAddress = "Extra_MacAddress";
    static final String Extra_Name = "Extra_Name";
    private String macAddress;

    //连接辅助类
    private BleConnectionHelper bleConnectionHelper;
    //RecyclerView适配器
    private BleConnectionAdapter mAdapter;


    //    创建一个附带参数的Intent方法
    public static Intent newIntent(Context context, String macAddress, String name) {
        final Intent intent = new Intent(context, BleClientPageActivity.class);
        intent.putExtra(Extra_MacAddress, macAddress);
        intent.putExtra(Extra_Name, name);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bleclient_page_activity);
        //获取Intent数据
        macAddress = getIntent().getStringExtra(Extra_MacAddress);
        TextView nameText = findViewById(R.id.nameText);
        String name = getIntent().getStringExtra(Extra_Name);
        nameText.setText(name);
        //初始化蓝牙(必须要在组件初始化之前)
        initBluetooth();
        //初始化视图组件
        initView();

    }

    /**
     * 初始化组件
     */
    private void initView() {
        //初始化下拉刷新布局
        SwipeRefreshLayout mSwipeRefreshLayout = findViewById(R.id.mSwipeRefreshLayout);
        mSwipeRefreshLayout.setColorSchemeColors(ContextCompat.getColor(this, R.color.colorPrimary));
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //刷新服务
                bleConnectionHelper.discoverServices();
            }
        });
        //禁止下拉

        mSwipeRefreshLayout.setEnabled(false);
        //初始化RecyclerView
        RecyclerView serviceRecyclerView = findViewById(R.id.serviceRecyclerView);
        serviceRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new BleConnectionAdapter(this, bleConnectionHelper);
        serviceRecyclerView.setAdapter(mAdapter);
    }

    //初始化蓝牙连接帮助对象及连接监听器
    private void initBluetooth() {
        bleConnectionHelper = new BleConnectionHelper(this, macAddress);

        bleConnectionHelper.setBleConnectionListener(new MyBleConnectionListener());
    }
    /**
     * 连接按钮点击监听方法，在layout布局文件中已经设置了onClick(View view)事件，指定为该方法名,但是不知道该方法的参数是如何确定的？
     */
    public void connectionBle(View view){
        System.out.println("点击按钮的事件接收到的view对象所属类："+view.getAccessibilityClassName());
        Button connectionBtn = view.findViewById(R.id.connectionBtn);
        connectionBtn.setEnabled(false);
        if(!bleConnectionHelper.isConnected()){
            //连接
            bleConnectionHelper.connection();
        }else{
            //断开连接
            bleConnectionHelper.disConnection();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        bleConnectionHelper.onDestroy();
    }

    /**
     * 内部类，实现监听接口
     */
    class MyBleConnectionListener implements BleConnectionHelper.BleConnectionListener {
        @Override
        public void onConnectionSuccess() {
            Button connectionBtn = findViewById(R.id.connectionBtn);
            connectionBtn.setText("断开连接");
            connectionBtn.setEnabled(true);
            //启动下拉
            SwipeRefreshLayout mSwipeRefreshLayout = findViewById(R.id.mSwipeRefreshLayout);
            mSwipeRefreshLayout.setEnabled(true);
            ToastUtils.showBottomToast(BleClientPageActivity.this, "连接成功");
        }

        @Override
        public void onConnectionFail() {

            Button connectionBtn = findViewById(R.id.connectionBtn);
            connectionBtn.setEnabled(true);
            ToastUtils.showBottomToast(BleClientPageActivity.this, "连接失败");
        }

        @Override
        public void disConnection() {
            Button connectionBtn = findViewById(R.id.connectionBtn);
            connectionBtn.setText("连接");
            connectionBtn.setEnabled(true);
            //禁止下拉
            SwipeRefreshLayout mSwipeRefreshLayout = findViewById(R.id.mSwipeRefreshLayout);
            mSwipeRefreshLayout.setEnabled(false);
            //清空列表
            BleConnectionHelper.getMGattServiceList().clear();
            mAdapter.notifyDataSetChanged();
            ToastUtils.showBottomToast(BleClientPageActivity.this, "断开连接");
        }

        @Override
        public void discoveredServices() {
            //发现服务完成后
            SwipeRefreshLayout mSwipeRefreshLayout = findViewById(R.id.mSwipeRefreshLayout);
            if (mSwipeRefreshLayout.isRefreshing()) {
                mSwipeRefreshLayout.setRefreshing(false);
            }
            //刷新列表
            mAdapter.notifyDataSetChanged();
            ToastUtils.showBottomToast(BleClientPageActivity.this, "发现服务");
        }

        @Override
        public void readCharacteristic(String data) {
            ToastUtils.showBottomToast(BleClientPageActivity.this, data);
        }

        @Override
        public void writeCharacteristic(String data) {
            ToastUtils.showBottomToast(BleClientPageActivity.this, data);
        }

        @Override
        public void readDescriptor(String data) {
            ToastUtils.showBottomToast(BleClientPageActivity.this, data);
        }

        @Override
        public void writeDescriptor(String data) {
            ToastUtils.showBottomToast(BleClientPageActivity.this, data);
        }

        @Override
        public void characteristicChange(String data) {
            //ToastUtils.showBottomToast(this@BleClientPageActivity,data)
            //使用普通Toast  让写入回调Toast和特征改变Toast能完整展示
            Toast.makeText(BleClientPageActivity.this, data, Toast.LENGTH_SHORT).show();
        }
    }
}
